package com.scalar.chiptrack.sales.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.process.dao.FabDAO;
import com.scalar.chiptrack.operations.process.FabStageGroup;
import com.scalar.chiptrack.sales.forms.TopCustomersForm;
import com.scalar.chiptrack.sales.TopCustomers;
import com.scalar.chiptrack.sales.TopCustomer;
import com.scalar.chiptrack.sales.dao.TopCustomersDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;

public class TopCustomersAndEndCustomersAction extends ChipTrackAction {

    public static final String ACTION_NAME = "/topCustomers.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception {

        TopCustomersForm topCustomersForm = ( TopCustomersForm ) form;
        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if ( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?"+ request.getQueryString() );
            return mapping.findForward("login");
        }
        ArrayList customersList = new ArrayList();
        String action = request.getParameter("action");
        HashMap info   = new HashMap();
        HashMap errors = new HashMap();
        request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );

        TopCustomers topCustomers = null;
        TopCustomer  topCustomer = topCustomersForm.getTopCustomer();
        ArrayList selectedTopCustomersList = new ArrayList();
        ArrayList ranksList = new ArrayList();
        ArrayList customersRanksList = new ArrayList();

        if ( action == null )
        {
        }
        if ( "loadCustomerTypes".equalsIgnoreCase( action ) )
        {
            request.setAttribute("customerTypes", topCustomersForm.getCustpmerTypes());
            request.setAttribute("showCustomers", "false");
            return mapping.findForward( "success" );
        }
        if ("loadTopCustomers".equalsIgnoreCase(action))
        {
            String customerType = topCustomersForm.getCustomerType();
            if (customerType.equalsIgnoreCase("1"))
            {
                customersRanksList = TopCustomersDAO.loadCustomersRanks( 1);
                customersList = TopCustomersDAO.loadAvailableCustomers(null, 1);
            }
            else if (customerType.equalsIgnoreCase("2"))
            {
                customersRanksList = TopCustomersDAO.loadCustomersRanks(2);
                customersList = TopCustomersDAO.loadAvailableCustomers(null, 2);
            }
                if ( customersRanksList.size() > 0 )
                {
                    //fabStage.setTotalGroupNo( stageGroupsList.size() );
                    for ( int i = 0; i < customersRanksList.size(); i++ )
                    {
                        topCustomers = (TopCustomers) customersRanksList.get ( i );
                        selectedTopCustomersList = TopCustomersDAO.loadExistingCustomers( null, topCustomers.getRank(), topCustomers.getCTypeId() );
                        topCustomers.setSelectedCustomersList( selectedTopCustomersList );

                    }
                    topCustomers = ( TopCustomers ) customersRanksList.get ( 0 );
                    topCustomers.setAvailableCustomersList( customersList );
                    if (customersRanksList.size() <=19)
                    {
                        for ( int i = customersRanksList.size()+1; i <=20; i++ )
                        {
                            topCustomers = new TopCustomers();
                            topCustomers.setRank(i);
                            customersRanksList.add(topCustomers);
                        }
                    }
                }
                else
                {
                    for ( int i = customersRanksList.size()+1; i <=20; i++ )
                    {
                        topCustomers = new TopCustomers();
                        topCustomers.setRank(i);
                        customersRanksList.add(topCustomers);
                    }
                }

            for ( int i = 1; i <=20; i++ )
            {
                ranksList.add( new LabelValueBean( i+"", i+"" ) );
            }

            topCustomer.setTotalRanks(customersRanksList.size());
            topCustomer.setTopCustomers(customersRanksList);
            topCustomersForm.setTopCustomer(topCustomer);
            request.setAttribute("ranksList" , ranksList);
            request.setAttribute("customerTypes", topCustomersForm.getCustpmerTypes());
            request.setAttribute("availableCustomersList", customersList);
            request.setAttribute("showCustomers", "true");

        }
        if ( "saveTopCustomers".equalsIgnoreCase( action ) )
        {
            DBTransaction dbTransaction = null;
            boolean success = false;
            try
            {
                dbTransaction = DBTransaction.getInstance ();
                dbTransaction.begin ();
                topCustomer= topCustomersForm.getTopCustomer();
                if (topCustomersForm.getCustomerType().equalsIgnoreCase("1"))
                {
                    topCustomer.setCTypeId(1);
                }
                else if (topCustomersForm.getCustomerType().equalsIgnoreCase("2"))
                {
                    topCustomer.setCTypeId(2);
                }
                TopCustomersDAO.saveTopCustomers( dbTransaction, topCustomer );
                topCustomersForm.setTopCustomer( topCustomer );
                success = true;
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    if( dbTransaction != null )
                    {
                        if( success == true )
                        {
                            dbTransaction.commit();
                            info.put ("Top 20 Customers have been saved successfully", null);
                        }
                        else
                        {
                            dbTransaction.rollback();
                            errors.put ("Unable to save top 20 customers into Data Base", null);
                        }
                    }
                }
                catch(Exception e1)
                {
                    e1.printStackTrace();
                }
            }
            if (topCustomersForm.getCustomerType() .equalsIgnoreCase("1"))
            {
                customersRanksList = TopCustomersDAO.loadCustomersRanks( 1);
                customersList = TopCustomersDAO.loadAvailableCustomers(null, 1);
            }
            else if (topCustomersForm.getCustomerType().equalsIgnoreCase("2"))
            {
                customersRanksList = TopCustomersDAO.loadCustomersRanks(2);
                customersList = TopCustomersDAO.loadAvailableCustomers(null, 2);
            }
                        if ( customersRanksList.size() > 0 )
                        {
                            //fabStage.setTotalGroupNo( stageGroupsList.size() );
                            for ( int i = 0; i < customersRanksList.size(); i++ )
                            {
                                topCustomers = (TopCustomers) customersRanksList.get ( i );
                                selectedTopCustomersList = TopCustomersDAO.loadExistingCustomers( null, topCustomers.getRank(), topCustomers.getCTypeId() );
                                topCustomers.setSelectedCustomersList( selectedTopCustomersList );
                            }
                            topCustomers = ( TopCustomers ) customersRanksList.get ( 0 );
                            topCustomers.setAvailableCustomersList( customersList );
                            if (customersRanksList.size() <=20)
                            {
                                for ( int i = customersRanksList.size()+1; i <=20; i++ )
                                {
                                    topCustomers = new TopCustomers();
                                    topCustomers.setRank(i);
                                    customersRanksList.add(topCustomers);
                                }
                            }
                        }
                        else
                        {

                            for ( int i = customersRanksList.size()+1; i <=20; i++ )
                            {
                                topCustomers = new TopCustomers();
                                topCustomers.setRank(i);
                                customersRanksList.add(topCustomers);
                            }
                        }

                    for ( int i = 1; i <=20; i++ )
                    {
                        ranksList.add( new LabelValueBean( i+"", i+"" ) );
                    }

                    topCustomer.setTotalRanks(customersRanksList.size());
                    topCustomer.setTopCustomers(customersRanksList);
                    topCustomersForm.setTopCustomer(topCustomer);
                    request.setAttribute("ranksList" , ranksList);
                    request.setAttribute("customerTypes", topCustomersForm.getCustpmerTypes());
                    request.setAttribute("availableCustomersList", customersList);
                    request.setAttribute("showCustomers", "true");
        }
        return mapping.findForward( "success" );

    }
}